(function($) {
	var nurseid = '',sourceData,locSchArr;
	var wardID,startDate,endDate="";
	mui.init();
	mui.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function isExsitArg(isLastWeek,lunCheckBlock){
		//console.log('isExsitArg')
		var pdict = {'parr':wardID+'^'+startDate}
		if (isLastWeek) {
			var lastWeekDate = getLastWeekStartDate(startDate)
			pdict = {'parr':wardID+'^'+lastWeekDate}
		}
		var paramDict = {'method':'web.INMArgComm:IsExsitArg','params':JSON.stringify(pdict),'type':'Method'}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				return
			}
			var id = parseInt(result['msg'])
			
			//getLocSchData();
		}, function(errorStr) {
			
		});
	}
	
	function getLocSchData(isLastWeek){
		var pdict = {'nurseid':nurseid,'parr':wardID+'^'+startDate+'^'+endDate}
		if (isLastWeek) {
			var lastWeekDate = getLastWeekStartDate(startDate)
			var lastEnd = getLastWeekStartDate(endDate)
			pdict = {'parr':wardID+'^'+lastWeekDate+'^'+lastEnd}
		}
		
		var paramDict = {'method':'web.INMArgComm:FindArrangeSubList','params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			locSchArr = result['data']
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				
				mui.toast(result['msg'])
				return
			}
			
			updateLocSchUI(locSchArr)
			
		}, function(errorStr) {
			clearLocSchUI()
			showNullTip('哎呀,服务器逃跑了。<div id="refreshBtn">刷新试试</div>')
		});
	}
	function updateHeaderUI(){
		var trDom = $('#locHeadDate')[0]
		var html = '<td data-id="'+startDate+'">'+startDate.substring(startDate.length - 2,startDate.length)+'</td>'
		var date = startDate
		for (var i = 0; i < 5;i++) {
			date = getNextDay(date)
			html += '<td data-id="'+date+'">'+date.substring(date.length - 2,date.length)+'</td>'
		}
		html += '<td data-id="'+endDate+'">'+endDate.substring(endDate.length - 2,endDate.length)+'</td>'
		trDom.innerHTML = html
	}
	
	function updateLocSchUI(dataArr){
		var html = ''
		mui.each(dataArr,function (index,oneDict) {
			if (oneDict['PerID'] == undefined) {
				html += '<tr><td colspan="8" class="groupName">'+oneDict['PerName'];
				if (oneDict['bedCodes']!="") {
					html += '（'+oneDict['bedCodes']+'）';
				}
				html += '</td></tr>';
				return;
			}
			html += '<tr id="row-'+index+'" data-PerID="'+oneDict['PerID']+'" class="tr-nur"><td>'+oneDict['PerName'].replace(oneDict['PerID'],"")+'</td>';
			mui('#locHeadDate td').each(function (index2,headTd) {
				var date = headTd.getAttribute('data-id')
				html +=getOneSchHtml(oneDict,date)
			})
			html+='</tr>';
		})
		$('#locBody')[0].innerHTML = html;
		var lastSelectDom = $(lastSelect)[0]
		if (lastSelectDom) {
			mui.trigger(lastSelectDom,'tap')
		}else{
			lastSelect = ''
		}
	}
	
	/*//3943」N1『16:00-次日00:00「3944」休『%% 
	* 解析:id」班次描述及备注『一段时间;二段时间;三段时间「第二个班次……%请假班次描述%意愿排班申请id」班次描述「第二个意愿排班……
	* 先用%分割:0为已排班次信息,1为请假信息,2为意愿排班信息
	* 对于已排班次信息:多个班次之间用「分割,单个班次内用」分割,0为已排班次id,1为已排班次其他信息
	* 对于意愿排班信息:多个意愿排班用「分割, 单个班次内用」分割,0为意愿排班申请id,1为班次描述
	* 有请假信息 该单元格不允许添加排班  有意愿排班 点击的时候需要弹出审核框
	* //#0051FF!#3CFF00!@#0AF244!
	* 解析: 字体颜色!背景色 多个班次用@分隔
	*/
	function getOneSchHtml(oneDict,date){
		var str = oneDict['AppDate'+date]
		var colorStr = oneDict['Color'+date]
		var isHasMark = oneDict['AppRemark'+date]
		var html = ''
		str=!str?"":str
		colorStr=!colorStr?"":colorStr
		var schArr =  
		mui.each(str.split('%'),function (schIndex,oneSch) {
			var arr = oneSch.split('「')
			var colorArr = colorStr.split('@')
			mui.each(arr,function (index,oneStr) {
				var colorHtml = ''
				if (schIndex==0&& colorArr[index]!=undefined && colorArr[index] != "") {
					var oneColorArr = colorArr[index].split('!')
					colorHtml = 'style="'
					if (oneColorArr[1] != '') {
						colorHtml += 'background: '+oneColorArr[1]+';'
					}
					if (oneColorArr[0] != '') {
						colorHtml += 'color: '+oneColorArr[0]+';'
					}
					colorHtml += '"'
				}
				var tmpArr = oneStr.split('」')
				var textStr = ''
				var mainStr = tmpArr[1]
				if (mainStr != undefined && mainStr != "") {
					var mainArr = mainStr.split('『')
					html += '<div '+colorHtml+'>'+mainArr[0]+'、</div>';
				}
			})
		})
		html = html.replace(/(.*)、</, '$1<');
		var markClass = ''
		if (isHasMark == 'Y') {
			markClass = ' nurfont nur-zhushi'
		}
		return '<td data-day="'+date+'" data-sch="'+str+'" class="schText'+markClass+'">'+html+'</td>'
	}

	function baseSetup() {
		mui('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});

		var self = plus.webview.currentWebview();
		sourceData = self.sourceData;//{'wardID': 'wardDesc' 'startDate':startDate,'endDate':endDate,'rw':rw}
		wardID = sourceData['wardID']
		startDate = sourceData['startDate']
		endDate = sourceData['endDate']
		getLocSchData(true);
	}

	function showNullTip(msg, type) {
		mui('#nullData')[0].style.display = 'block';
		mui('#nullTip')[0].innerHTML = msg
	}

	function hideNullTip() {
		mui('#nullData')[0].style.display = 'none';
	}
})(mui)
